using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SBPweb.Modules.Modularity;
using SBPweb.Contracts;
using SBPweb.Contracts.Messaging;

using SBPweb.Controls.Windows.Docking;

using SBPweb.Presentation.Windows.Forms.Controls;
using SBPweb.Contracts.Implementation;

namespace SBPweb.Modules.Messaging
{
	public partial class LoginDockingControl : DockingUserControl
	{
		#region Internal properties

		internal string CurrentPassword
		{
			get
			{
				return loginControl.CurrentPassword;
			}
		}

		internal string CurrentUser
		{
			get
			{
				return loginControl.CurrentUser;
			}
		}

		internal string DirectoryServer
		{
			get
			{
				return loginControl.DirectoryServer;
			}
		}

		internal string ConferenceServer
		{
			get
			{
				return loginControl.ConferenceServer;
			}
		}

		internal string PubsubServer
		{
			get
			{
				return loginControl.PubsubServer;
			}
		}

		internal int Port
		{
			get
			{
				return loginControl.Port;
			}
		}

		internal bool IsSaveUserName
		{
			get
			{
				return loginControl.IsSaveUserName;
			}
		}

		internal bool IsSavePassword
		{
			get
			{
				return loginControl.IsSavePassword;
			}
		}

		#endregion

		#region Public members

		public IMessagingService DirectoryService
		{
			get
			{
				return ParentModule.Services.Get<IMessagingService>();
			}
		}

		#endregion

		#region Constructor logic

		public LoginDockingControl(ModuleBase parentModule)
			: base(parentModule)
		{
			InitializeComponent();
			this.Dock = DockStyle.Fill;
			IsModal = true;
			this.MinimumSize = new Size(311, 330);
			this.FloatingSize = new Size(311, 330);
			this.Size = new Size(311, 330);
			this.MaximumSize = new Size(311, 330);
			SBPweb.Localization.LocalizationExtender.OnInvalidatingControls += new EventHandler(LocalizationExtender_OnInvalidatingControls);

			(this as System.Windows.Forms.Control).Text = SBPweb.Localization.LocalizationExtender.GetString("LoginHeader");
            this.InitialAppearanceStyle = SBPweb.Modules.Smartparts.AppearanceStyle.Float;
			AllowFloat = true;
            AllowDockBottom = AllowDockCenter = AllowDockLeft = AllowDockRight = AllowDockTop = AllowDrop = false;
			
			DirectoryService.OnAutenticate += new ConnectionHandler(DirectoryService_OnAutenticate);
			DirectoryService.OnAuthenticationError += new ConnectionHandler(DirectoryService_OnAuthenticationError);
			DirectoryService.OnDisconnect += new ConnectionHandler(DirectoryService_OnDisconnect);
            DirectoryService.OnError += new ConnectionErrorHandler(DirectoryService_OnError);
		}

		#endregion

		#region Event handlers

		private void DirectoryService_OnDisconnect(object sender)
		{
            IsVisible = true;
		}

		private void DirectoryService_OnAuthenticationError(object sender)
		{
            IsVisible = true;
		}

		private void DirectoryService_OnAutenticate(object sender)
		{
            IsVisible = false;
		}

		private void DirectoryService_OnError(object sender, string message)
		{
			IsVisible = true;
		}

		private void LocalizationExtender_OnInvalidatingControls(object sender, EventArgs e)
		{
			(this as System.Windows.Forms.Control).Text = SBPweb.Localization.LocalizationExtender.GetString("LoginHeader");
		}

		#endregion

		#region Presitence handling

		public void SavePersistence(PersistencyDictionary state)
		{
			loginControl.SavePersistence(state);
		}

		public void LoadPersistence(PersistencyDictionary state)
		{
			loginControl.LoadPersistence(state);
		}

		#endregion
	}
}
